#!/usr/bin/evn python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   文件名称 :     app
   文件功能描述 :   功能描述
   创建人 :       小钟同学
   创建时间 :          2021/12/6
-------------------------------------------------
   修改描述-2021/12/6:         
-------------------------------------------------
"""

from grpcframe.zyxgrpc.base_app import FastGrpcApp
from grpcframe.exts.redis.client import sync_redis_client
from grpcframe.exts.register.consul import ConsulRegister
from grpcframe.zyxgrpc.base_middleware import BaseMiddleware,Any
import grpc

# 注册中心
register = ConsulRegister(host='127.0.0.1',port=8500, server_name='FastGrpcApp',server_id="23423424234")
# 实例化一个带有注册中心的服务对象
app = FastGrpcApp(port=45564,max_workers=20,is_certificate=False,register_center=None,debug=True)

@app.on_event('startup')
def startup():
    print("startup=========1》")
    sync_redis_client.init_app(app)

@app.on_event('shutdown')
def shutdown():
    print("shutdown=========》")


@app.add_middleware
class ServiceLogMiddleware2222(BaseMiddleware):

    def before_handler(self, request: Any, context: grpc.ServicerContext, method_name):
        pass
        print("我是自定义的中间件信息---》1")

    def after_handler(self, context: grpc.ServicerContext, response):
        pass
        print("我是自定义的中间件信息---》2")

    def error_grpc_exc_handler(self, error):
        pass

@app.on_event('startup')
def startup():
    print("startup=========3》")

# 添加事件处理
def ceshiguanbi():
    print("关闭了！")
app.add_event_handler(event_type='shutdown', func=ceshiguanbi)

# 添加服务处理
# app.add_servicer(Greeter)
from grpcframe.middlewares.tracer.middleware import Zyxopentracing
app.add_middleware(Zyxopentracing)

from example.hello_srv.middlewares.authinterceptor import AuthMiddleware
# PS: 关于中间件注册，最后注册，先执行，遵循的是洋葱模型,(需要统一基于Bsea等Middleware，原生的会首先执行！)
app.add_middleware(AuthMiddleware)
# 批量注册导入服务模块
app.load_servicers_handlers_in_module('example.hello_srv.servicers')




if __name__ == '__main__':
    # 单独的进行加载
    app.run_serve()
